import sys
from typing import Callable

from PyQt5.QtCore import pyqtSignal, QObject


# CoreProcessListener 作为基类、接口类，具体功能的实现由子类或其他事件接收者完成
# CoreProcessListener 和 CoreProcess 并没有做过多的规划，仅仅将旧的代码进行了少量的修改（CoreProcess 中去除直接调用UI的操作）
# 后续最好是对CoreProcess重新规划设计

class CoreProcessListener(QObject):
    __showSignal = pyqtSignal(list)
    __webCameraList = None

    __cbShowImg = None
    __cbSend2Plc = None

    def __init__(self):
        super().__init__()
        self.__webCameraList = []
        self.__showSignal.connect(self.__ShowImgDataEvent)

    def RegShowImgCb(self, cbFunc: Callable[[list], None]):
        self.__cbShowImg = cbFunc

    def RegSend2PlcCb(self, cbFunc):
        self.__cbSend2Plc = cbFunc

    def ShowImgData(self, data: list):
        self.__showSignal.emit(data)

    def __ShowImgDataEvent(self, data: list):
        #self._OnShowImgData(data)
        if self.__cbShowImg is not None:
            self.__cbShowImg(data)

    # def _OnShowImgData(self, data: list):
    #     print('图像及结果，请在子类中重写函数_OnShowImgData')

    def Send2Plc(self, regAddr, data):
        #self._OnSend2Plc(regAddr, data)
        if self.__cbSend2Plc is not None:
            self.__cbSend2Plc(regAddr, data)

    # def _OnSend2Plc(self, regAddr, data):
    #     print('给PLC发送数据，请在子类中重写函数_OnSend2Plc')
    #     # 之前是调用 doctr.write_register，请在子类中实现！

    def AppendWebCamera(self, camera):
        if self.__webCameraList is None:
            self.__webCameraList = [camera]
        else:
            self.__webCameraList.append(camera)

    def GetWebCameraList(self):
        return self.__webCameraList




